<?php 
class ControllerCatalogDownloadExtra extends Controller { 
	private $error = array();
 
	public function index() {
		$this->load->language('catalog/download_extra');

		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('catalog/download_extra');
		 
		$this->getList();
	}

	public function insert() {
		$this->load->language('catalog/download_extra');

		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('catalog/download_extra');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			
			$this->model_catalog_download_extra->addDownloadExtra($this->request->post);
			

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('catalog/download_extra', 'token=' . $this->session->data['token'] . $url, 'SSL')); 
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('catalog/download_extra');

		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('catalog/download_extra');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_catalog_download_extra->editDownloadExtra($this->request->get['download_extra_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');
			$url = '';
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('catalog/download_extra', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}
	

	public function delete() {
		$this->load->language('catalog/download_extra');

		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('catalog/download_extra');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $download_extra_id) {
				$this->model_catalog_download_extra->deleteDownloadExtra($download_extra_id);
			}
			$this->session->data['success'] = $this->language->get('text_success');
			$url = '';
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('catalog/download_extra', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}
	
	public function upload(){
		$json=array();
		if(isset($this->request->files['download_extra_file']) && isset($this->request->files['download_extra_file']['tmp_name'])){
			
			$download_extra_file=$this->request->files['download_extra_file'];
			$json['type']=$download_extra_file['type'];
			$extension_allow=array('application/octet-stream');
			$check_file_name = basename($download_extra_file['name']);
			$check_file_name = str_replace (' ', '-', $check_file_name);
			if(!isset($json['error'])){
				if(@move_uploaded_file($download_extra_file['tmp_name'], DIR_IMAGE.'/'.$check_file_name)){
					$json['success']=$this->language->get('text_upload_sucess');
					$json['file_download_name']=$check_file_name;
				}else{
					$json['error']=$this->language->get('error_save_file');
				}
			}
		}
		$this->response->setOutput(Json_encode($json));
	}
	
	private function getList() {
	
		$url = '';
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
	
		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('catalog/download_extra', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
      		'separator' => ' :: '
   		);
		
									
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=catalog/download_extra/insert&token=' . $this->session->data['token'];
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=catalog/download_extra/delete&token=' . $this->session->data['token'];
		
		$this->data['categories'] = array();
		$results=array();
		$results = $this->model_catalog_download_extra->getDownloadsExtra();
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=catalog/download_extra/update&token=' . $this->session->data['token'] . '&download_extra_id=' . $result['download_extra_id']
			);
					
			$this->data['categories'][] = array(
				'download_extra_id' => $result['download_extra_id'],
				'name'        => $result['name'],
				'sort_order'  => $result['sort_order'],
				'selected'    => isset($this->request->post['selected']) && in_array($result['download_extra_id'], $this->request->post['selected']),
				'action'      => $action
			);
		}
		
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$this->template = 'catalog/download_extra_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_upload'] = $this->language->get('text_upload');
		$this->data['text_link'] = $this->language->get('text_link');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_from_computer'] = $this->language->get('text_from_computer');
		$this->data['text_from_embed'] = $this->language->get('text_from_embed');
		$this->data['text_from_link'] = $this->language->get('text_from_link');
		$this->data['text_delete'] = $this->language->get('text_delete');
		$this->data['text_comment'] = $this->language->get('text_comment');
		$this->data['text_embed'] = $this->language->get('text_embed');
		$this->data['text_computer'] = $this->language->get('text_computer');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_short_description'] = $this->language->get('entry_short_description');
		$this->data['entry_meta_keywords'] = $this->language->get('entry_meta_keywords');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_oject'] = $this->language->get('entry_oject');
		$this->data['entry_from_link'] = $this->language->get('entry_from_link');
		
		$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_video'] = $this->language->get('entry_video');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_status'] = $this->language->get('entry_status');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

    	$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
    	$this->data['tab_comment'] = $this->language->get('tab_comment');		
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
	
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}
		if (isset($this->error['type'])) {
			$this->data['error_type'] = $this->error['type'];
		} else {
			$this->data['error_type'] = '';
		}
		if (isset($this->error['file_name'])) {
			$this->data['error_file_name'] = $this->error['file_name'];
		} else {
			$this->data['error_file_name'] = '';
		}
		$url = '';
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('catalog/download_extra', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
      		'separator' => ' :: '
   		);
  		if (!isset($this->request->get['download_extra_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/download_extra/insert&token=' . $this->session->data['token'];
			$this->data['download_extra_id'] = NULL;
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/download_extra/update&token=' . $this->session->data['token'] . '&download_extra_id=' . $this->request->get['download_extra_id'];
			$this->data['download_extra_id'] = $this->request->get['download_extra_id'];
		}
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=catalog/download_extra&token=' . $this->session->data['token'];

		$this->data['token'] = $this->session->data['token'];
			
		if (isset($this->request->get['download_extra_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$download_extra_info = $this->model_catalog_download_extra->getDownloadExtra($this->request->get['download_extra_id']);
    	}
		$this->load->model('localisation/language');
		
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		if (isset($this->request->post['category_download_extra_id'])) {
			$this->data['category_download_extra_id'] = $this->request->post['category_download_extra_id'];
		} elseif (isset($download_extra_info)) {
			$this->data['category_download_extra_id'] = $download_extra_info['category_download_extra_id'];
		} else {
			$this->data['category_download_extra_id'] = 0;
		}
		if (isset($this->request->post['download_extra_description'])) {
			$this->data['download_extra_description'] = $this->request->post['download_extra_description'];
		} elseif (isset($download_extra_info)) {
			$this->data['download_extra_description'] = $this->model_catalog_download_extra->getDownloadExtraDescriptions($this->request->get['download_extra_id']);
		} else {
			$this->data['download_extra_description'] = array();
		}
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (isset($download_extra_info)) {
			$this->data['status'] = $download_extra_info['status'];
		} else {
			$this->data['status'] = 1;
		}

		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (isset($download_extra_info)) {
			$this->data['keyword'] = $download_extra_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
		
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($download_extra_info)) {
			$this->data['image'] = $download_extra_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('tool/image');
		if (!empty($download_extra_info) && $download_extra_info['image'] && file_exists(DIR_IMAGE . $download_extra_info['image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($download_extra_info['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
		
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		
		
		if (isset($this->request->post['file_name'])) {
			$this->data['file_name'] = $this->request->post['file_name'];
		} elseif (isset($download_extra_info)) {
			$this->data['file_name'] = $download_extra_info['file_name'];
		} else {
			$this->data['file_name'] = '';
		}
		
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} elseif (isset($download_extra_info)) {
			$this->data['sort_order'] = $download_extra_info['sort_order'];
		} else {
			$this->data['sort_order'] = 0;
		}
		
		$this->template = 'catalog/download_extra_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
		public function deletefileDownloadExtra()
	{
		$this->load->model('catalog/download_extra');
		unlink(DIR_IMAGE.'/'.basename($this->request->get['file_name']));
		$this->model_catalog_download_extra->DeleteFileDownload($this->request->get['download_extra_id']);
		$this->response->setOutput('');
	}
	
	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'catalog/download_extra')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		foreach ($this->request->post['download_extra_description'] as $language_id => $value) {
			if ((strlen(utf8_decode($value['name'])) < 2) || (strlen(utf8_decode($value['name'])) > 255)) {
				$this->error['name'][$language_id] = $this->language->get('error_name');
			}
		}
		if($this->request->post['file_name']==""){
			$this->error['file_name']= $this->language->get('error_file_name');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			if (!isset($this->error['warning'])) {
				$this->error['warning'] = $this->language->get('error_required_data');
			}
			
			return FALSE;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'catalog/download_extra')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
 
		if (!$this->error) {
			return TRUE; 
		} else {
			return FALSE;
		}
	}
}
?>